{#if sensitive }
<div class={computedClass} style={customSize}>
  <div class="status-sensitive-inner-div">
    {#if sensitiveShown}
      <button id={elementId}
              type="button"
              class="status-sensitive-media-button"
              aria-label="{intl.hideSensitiveMedia}"
              ref:hideSensitiveMedia
      >
        <div class="svg-wrapper">
          <SvgIcon className="status-sensitive-media-svg"
                   href="#fa-eye-slash" />
        </div>
      </button>
    {:else}
      <button id={elementId}
              type="button"
              class="status-sensitive-media-button"
              aria-label="{intl.showSensitiveMedia}"
              ref:showSensitiveMedia
      >

        <div class="status-sensitive-media-warning">
          <div class="status-sensitive-media-warning-text">
            {intl.clickToShowSensitive}
          </div>
        </div>
        <div class="svg-wrapper">
          <SvgIcon className="status-sensitive-media-svg" href="#fa-eye" />
        </div>
      </button>
    {/if}
    <MediaAttachments {mediaAttachments} {sensitive} {sensitiveShown} {uuid} />
  </div>
</div>
{#if enableShortcuts}
  <Shortcut scope={shortcutScope} key="y" on:pressed="toggleSensitiveMedia(false)"/>
{/if}
{:else}
  <MediaAttachments {mediaAttachments} {sensitive} {sensitiveShown} {uuid} />
{/if}
{#if enableShortcuts}
  <Shortcut scope={shortcutScope} key="i" on:pressed="showFirstMedia()" />
{/if}
<style>
  .status-sensitive-media-container {
    grid-area: media;
    width: 100%;
    margin: 10px 0;
    position: relative;
    border-radius: 0;
    border: none;
    background: none;
  }

  .status-sensitive-inner-div {
    height: 100%;
  }

  .status-sensitive-media-container.grouped-images .status-sensitive-inner-div {
    position: relative;
    width: 100%;
  }

  .status-sensitive-media-container.grouped-images {
    grid-area: media-grp;
  }

  .status-sensitive-media-button {
    margin: 0;
    padding: 0;
    border: none;
    background: none;
  }

  .status-sensitive-media-button:hover {
    background: none;
  }

  .status-sensitive-media-button:active {
    background: none;
  }

  .status-sensitive-media-shown .status-sensitive-media-button {
    position: absolute;
    top: 5px;
    left: 5px;
    z-index: 90;
  }

  .status-sensitive-media-hidden .status-sensitive-media-button {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
  }

  .status-sensitive-media-container.status-sensitive-media-hidden {
    width: 100%;
    margin: 10px auto;
  }
  .status-sensitive-media-container.status-sensitive-media-hidden.not-grouped-images {
      height: 250px;
  }

  .status-sensitive-media-container .status-sensitive-media-warning {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--deemphasized-text-color);
    z-index: 60;
    padding: 0 10px;
    position: absolute;
  }

  .status-sensitive-media-container .status-sensitive-media-warning .status-sensitive-media-warning-text {
    background: var(--mask-bg);
    padding: 10px;
    border-radius: 6px;
    color: var(--blurhash-sensitive-text-color);
  }

  .status-sensitive-media-container .svg-wrapper {
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    z-index: 40;
    pointer-events: none;
    background: var(--mask-bg);
  }
  .status-sensitive-media-hidden .svg-wrapper {
    position: absolute;
    background: none;
    top: 5px;
    left: 5px;
    right: 0;
    bottom: 0;
  }

  .status-sensitive-media-container.grouped-images .svg-wrapper {
    border-radius: 6px;
  }
  .status-sensitive-media-container.status-sensitive-media-shown .svg-wrapper {
    background: none;
  }
  :global(.status-sensitive-media-svg) {
    width: 24px;
    height: 24px;
    fill: var(--mask-svg-fill);
    border-radius: 2px;
    background: var(--mask-opaque-bg);
    margin: 5px;
    padding: 6px 10px;
  }
  :global(.status-sensitive-media-container.status-sensitive-media-hidden .status-sensitive-media-svg) {
    fill: var(--blurhash-sensitive-text-color);
    background: var(--mask-bg);
  }
</style>
<script>
  import MediaAttachments from './MediaAttachments.html'
  import Shortcut from '../shortcut/Shortcut.html'
  import SvgIcon from '../SvgIcon.html'
  import { store } from '../../_store/store.js'
  import { registerClickDelegate } from '../../_utils/delegate.js'
  import { classname } from '../../_utils/classname.js'
  import { importShowMediaDialog } from '../dialog/asyncDialogs/importShowMediaDialog.js'

  export default {
    oncreate () {
      const { elementId } = this.get()
      registerClickDelegate(this, elementId, () => this.toggleSensitiveMedia(true))
    },
    components: {
      MediaAttachments,
      Shortcut,
      SvgIcon
    },
    store: () => store,
    computed: {
      computedClass: ({ sensitiveShown, oddCols, twoCols, $largeInlineMedia }) => classname(
        'status-sensitive-media-container',
        sensitiveShown ? 'status-sensitive-media-shown' : 'status-sensitive-media-hidden',
        oddCols && 'odd-cols',
        twoCols && 'two-cols',
        $largeInlineMedia ? 'not-grouped-images' : 'grouped-images'
      ),
      mediaAttachments: ({ originalStatus }) => originalStatus.media_attachments,
      sensitiveShown: ({ $sensitivesShown, uuid }) => !!$sensitivesShown[uuid],
      sensitive: ({ originalStatus, $markMediaAsSensitive, $neverMarkMediaAsSensitive }) => (
        !$neverMarkMediaAsSensitive && ($markMediaAsSensitive || originalStatus.sensitive)
      ),
      elementId: ({ uuid }) => `sensitive-${uuid}`,
      customSize: ({ $largeInlineMedia, mediaAttachments }) => {
        if ($largeInlineMedia || !mediaAttachments || mediaAttachments.length < 5) {
          return ''
        }
        return `padding-bottom: ${Math.ceil(mediaAttachments.length / 2) * 29}%;`
      },
      nCols: ({ mediaAttachments, $largeInlineMedia }) => {
        return (!$largeInlineMedia && mediaAttachments.length > 1) ? 2 : 1
      },
      oddCols: ({ mediaAttachments }) => {
        return (mediaAttachments.length > 1 && (mediaAttachments.length % 2))
      },
      twoCols: ({ mediaAttachments }) => {
        return (mediaAttachments.length === 2)
      }
    },
    methods: {
      toggleSensitiveMedia (changeFocus) {
        const { uuid } = this.get()
        const { sensitivesShown } = this.store.get()
        sensitivesShown[uuid] = !sensitivesShown[uuid]
        this.store.set({ sensitivesShown })
        this.fire('recalculateHeight')
        // Only change focus for clicks, not for hotkeys. It's weird if, when the entire toot
        // is focused and you press "y", that the focus changes to the sensitive media button.
        if (changeFocus) {
          requestAnimationFrame(() => {
            const element = this.refs.hideSensitiveMedia || this.refs.showSensitiveMedia
            try {
              element.focus({ preventScroll: true })
            } catch (e) {
              console.error('ignored focus error', e)
            }
          })
        }
        return true
      },
      async showFirstMedia () {
        const { mediaAttachments } = this.get()
        const showMediaDialog = await importShowMediaDialog()
        showMediaDialog(mediaAttachments, 0)
      }
    }
  }
</script>
